/**
 * 
 */
package com.precisosoft.mathematize.client.view;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.precisosoft.mathematize.client.Mathematize;
import com.precisosoft.mathematize.client.presenter.MainPagePresenter;
import com.precisosoft.mathematize.client.view.handlers.MainPageUiHandlers;
import com.precisosoft.mathematize.client.widgets.ApplicationMenu;
import com.precisosoft.mathematize.client.widgets.Masthead;
import com.precisosoft.mathematize.client.widgets.NavigationPane;
import com.precisosoft.mathematize.client.widgets.NavigationPaneHeader;
import com.precisosoft.mathematize.client.widgets.NavigationPaneSection;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickEvent;
import com.smartgwt.client.widgets.layout.events.SectionHeaderClickHandler;
import com.smartgwt.client.widgets.menu.Menu;

/**
 * @author jewettdx
 *
 */
public class MainPageView extends ViewWithUiHandlers<MainPageUiHandlers> implements
MainPagePresenter.MyView {

// NORTH_HEIGHT = MASTHEAD_HEIGHT + APPLICATION_MENU_HEIGHT + NAVIGATION_PANE_HEADER_HEIGHT
private static final int NORTH_HEIGHT = 85;
private static final int DEFAULT_MENU_WIDTH = 70;
private static final String DEFAULT_MARGIN = "0px";

private final Masthead masthead;
private final ApplicationMenu applicationMenu;
private final NavigationPaneHeader navigationPaneHeader;
private final NavigationPane navigationPane;

private VLayout panel;
private HLayout northLayout;
private HLayout southLayout;
private VLayout westLayout;

@Inject
public MainPageView(Masthead masthead, ApplicationMenu applicationMenu,
  NavigationPaneHeader navigationPaneHeader, NavigationPane navigationPane) {
this.masthead = masthead;
this.applicationMenu = applicationMenu;
this.navigationPaneHeader = navigationPaneHeader;
this.navigationPane = navigationPane;

// get rid of scroll bars, and clear out the window's built-in margin,
// because we want to take advantage of the entire client area
Window.enableScrolling(false);
Window.setMargin(DEFAULT_MARGIN);

// initialise the main layout container
panel = new VLayout();
panel.setWidth100();
panel.setHeight100();

// initialise the North layout container
northLayout = new HLayout();
northLayout.setHeight(NORTH_HEIGHT);

// initApplicationMenu();

// initialise the nested layout container
VLayout vLayout = new VLayout();
vLayout.addMember(this.masthead);
vLayout.addMember(this.applicationMenu);
vLayout.addMember(this.navigationPaneHeader);

// add the nested layout container to the North layout container
northLayout.addMember(vLayout);

// initNavigationPane();

// initialise the West layout container
westLayout = this.navigationPane;

// initialise the South layout container
southLayout = new HLayout();

// add the North and South layout containers to the main layout container
panel.addMember(northLayout);
panel.addMember(southLayout);

bindCustomUiHandlers();
}

// as per NavigationPaneDataSource
private static final String NAME = "name";

/**
* SmartGWT Event and GWT Handler Mapping should be done here.
*/
protected void bindCustomUiHandlers() {

// initialise the ToolBar and register its handlers
initApplicationMenu();

// initialise the NavigationPane and register its handlers
initNavigationPane();
}

private void onRecordClicked(RecordClickEvent event) {
Record record = event.getRecord();
String place = record.getAttributeAsString(NAME);

if (getUiHandlers() != null) {
  getUiHandlers().onNavigationPaneSectionClicked(place);
}
}

//@Override
public Widget asWidget() {
return panel;
}

@Override
public void setInSlot(Object slot, Widget content) {
Log.debug("setInSlot()");

if (slot == MainPagePresenter.TYPE_SetContextAreaContent) {
  if (content != null) {
    southLayout.setMembers(westLayout, (VLayout) content);
  }
} else {
  super.setInSlot(slot, content);
}
}

@Override
public void removeFromSlot(Object slot, Widget content) {
super.removeFromSlot(slot, content);

Log.debug("removeFromSlot()");
}

private void initApplicationMenu() {

applicationMenu.addMenu(Mathematize.getConstants().newActivityMenuName(),
    DEFAULT_MENU_WIDTH, Mathematize.getConstants().newActivityMenuItemNames(),
    null);
applicationMenu.addMenu(Mathematize.getConstants().newRecordMenuName(),
    DEFAULT_MENU_WIDTH, Mathematize.getConstants().newRecordMenuItemNames(),
    null);
Menu goToMenu = applicationMenu.addMenu(Mathematize.getConstants().goToMenuName(),
    DEFAULT_MENU_WIDTH - 30);
applicationMenu.addSubMenu(goToMenu, Mathematize.getConstants().salesMenuItemName(),
    Mathematize.getConstants().salesMenuItemNames(),
    null);
applicationMenu.addSubMenu(goToMenu, Mathematize.getConstants().settingsMenuItemName(),
    Mathematize.getConstants().settingsMenuItemNames(),
    null);
applicationMenu.addSubMenu(goToMenu, Mathematize.getConstants().resourceCentreMenuItemName(),
    Mathematize.getConstants().resourceCentreMenuItemNames(),
    null);
applicationMenu.addMenu(Mathematize.getConstants().toolsMenuName(),
    DEFAULT_MENU_WIDTH - 30, Mathematize.getConstants().toolsMenuItemNames(),
    null);
applicationMenu.addMenu(Mathematize.getConstants().helpMenuName(),
    DEFAULT_MENU_WIDTH - 30, Mathematize.getConstants().helpMenuItemNames(),
    null);
}

private void initNavigationPane() {

//navigationPane.addSection(Mathematize.getConstants().salesStackSectionName(),
//    SalesNpsDataSource.getInstance());
//navigationPane.addSection(Mathematize.getConstants().settingsStackSectionName(),
//    SettingsNpsDataSource.getInstance());
//navigationPane.addSection(Mathematize.getConstants().resourceCentreStackSectionName(),
//    ResourceCentreNpsDataSource.getInstance());

navigationPane.addSectionHeaderClickHandler(new SectionHeaderClickHandler() {
  //@Override
  public void onSectionHeaderClick(SectionHeaderClickEvent event) {
    SectionStackSection section = event.getSection();
    String name = ((NavigationPaneSection) section).getSelectedRecord();

    // If there is no selected record (e.g. the data hasn't finished loading)
    // then getSelectedRecord() will return an empty string.
    if (name.isEmpty()) {
//      if (section.getTitle().equals(Mathematize.getConstants().settingsStackSectionName())) {
//        // default to the first item e.g. "Administration" in Settings
//        name = SettingsNpsDataSource.DEFAULT_RECORD_NAME;
//      } else if (section.getTitle().equals(Mathematize.getConstants().resourceCentreStackSectionName())) {
//        // default to the first item e.g. "Highlights" in Resource Centre
//        name = ResourceCentreNpsDataSource.DEFAULT_RECORD_NAME;
//      }
    }

    if (getUiHandlers() != null) {
      getUiHandlers().onNavigationPaneSectionHeaderClicked(name);
    }
  }
});

navigationPane.addRecordClickHandler(Mathematize.getConstants().salesStackSectionName(),
    new RecordClickHandler() {
  //@Override
  public void onRecordClick(RecordClickEvent event) {
    onRecordClicked(event);
  }
});

navigationPane.addRecordClickHandler(Mathematize.getConstants().settingsStackSectionName(),
    new RecordClickHandler() {
  //@Override
  public void onRecordClick(RecordClickEvent event) {
    onRecordClicked(event);
  }
});

navigationPane.addRecordClickHandler(Mathematize.getConstants().resourceCentreStackSectionName(),
    new RecordClickHandler() {
  //@Override
  public void onRecordClick(RecordClickEvent event) {
    onRecordClicked(event);
  }
});
}

public ApplicationMenu getApplicationMenu() {
return applicationMenu;
}

public NavigationPaneHeader getNavigationPaneHeader() {
return navigationPaneHeader;
}

public NavigationPane getNavigationPane() {
return navigationPane;
}

}
